{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [],
   "source": [
    "#include <algorithm>  // std::unique\n",
    "#include <cctype>     // std::isspace\n",
    "#include <iostream>   // std::cout\n",
    "#include <string>     // std::string\n",
    "#include <utility>    // std::move\n",
    "\n",
    "// 通用工具：根据传入的“相邻字符判定”规则，压缩字符串中的重复片段。\n",
    "// BinaryPredicate 需满足形如 bool(char, char) 的可调用对象（函数、lambda、仿函数等）。\n",
    "template <typename BinaryPredicate>\n",
    "void remove_adjacent_duplicates(std::string& text, BinaryPredicate should_merge) {\n",
    "    // std::unique 会根据 should_merge 判定相邻字符是否“重复”\n",
    "    // 若 should_merge(lhs, rhs) 返回 true，则 rhs 会被视为重复并移动到序列尾部\n",
    "    auto new_end = std::unique(text.begin(), text.end(), should_merge);\n",
    "\n",
    "    // 删除尾部多余的字符，真正缩短字符串\n",
    "    text.erase(new_end, text.end());\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After whitespace collapse:\n",
      "This is a test.\n",
      "New line.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "{\n",
    "    // 原始文本：包含多个连续的空格、制表符和换行\n",
    "    std::string text = \"This   is   \\t a   test.\\n\\nNew   line.\";\n",
    "\n",
    "    // 1. 定义一个可复用的判定器：用于识别相邻的空白字符\n",
    "    //    将该 lambda 与 std::unique 解耦，便于在其他场景下替换 / 复用。\n",
    "    auto collapse_whitespace = [](char lhs, char rhs) {\n",
    "        // 注意：std::isspace 的参数需要转换为 unsigned char，防止符号扩展带来的未定义行为\n",
    "        return std::isspace(static_cast<unsigned char>(lhs)) &&\n",
    "               std::isspace(static_cast<unsigned char>(rhs));\n",
    "    };\n",
    "\n",
    "    // 使用自定义判定器压缩字符串\n",
    "    remove_adjacent_duplicates(text, collapse_whitespace);\n",
    "    std::cout << \"After whitespace collapse:\\n\" << text << \"\\n\\n\";\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "vscode": {
     "languageId": "cpp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "After dash collapse:\n",
      "Phone: 123-456-7890\n"
     ]
    }
   ],
   "source": [
    "{\n",
    "    // 2. 展示如何自定义其他判定规则：\n",
    "    //    下面的示例会把所有连续的 '-' 压缩成一个 '-'\n",
    "    std::string dashed = \"Phone: 123----456---7890\";\n",
    "    auto collapse_dashes = [](char lhs, char rhs) {\n",
    "        return lhs == '-' && rhs == '-';\n",
    "    };\n",
    "\n",
    "    remove_adjacent_duplicates(dashed, collapse_dashes);\n",
    "    std::cout << \"After dash collapse:\\n\" << dashed << '\\n';\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
